/**
 * This package contains all classes used to translate from the server side classes to the shared 
 * Communication classes 
 */
package com.aceitunaproject.server.translator;

import java.util.LinkedList;
import java.util.List;

import com.aceitunaproject.shared.map.MapTile;
import com.aceitunaproject.shared.map.TerrainType;


/**
 * @author Federico Bergstein
 * 
 *         Translates MapTile server class instances to Shared MapTile class
 *         instances
 */
public class TileTranslator {

	/**
	 * Translates a server side MapTile to a shared MapTile
	 * 
	 * @param server
	 *            side MapTile
	 * @return shared MapTile
	 */
	public static MapTile toShared(com.aceitunaproject.server.map.MapTile from) {
		MapTile to = new MapTile();

		to.setPosx(from.getXPos());
		to.setPosy(from.getYPos());
		for (com.aceitunaproject.server.terrain.TerrainType tt : from
				.getTerrainTypes().values()) {

			to.getTerrain().add(
					new TerrainType(tt.getDefaultName(), tt.getDefaultName()));

		}

		to.setUnits(UnitTranslator.ListToShared(from.getUnitIDs()));
		
		if (from.getSettlement() != null) {
			to.setSettlement(SettlementTranslator
					.toShared(from.getSettlement()));
		}
		
		to.setInstallations(InstallationTranslator.ListToShared(from.getInstallations()));
		
		return to;

	}

	/**
	 * 
	 * Translates a list of Server side MapTile objects to a List of Shared
	 * MapTile objects
	 * 
	 * @param List
	 *            of Server side MapTile objects
	 * @return List of Shared MapTile Objects
	 */
	public static List<MapTile> ListToShared(
			List<com.aceitunaproject.server.map.MapTile> from) {
		LinkedList<MapTile> retList = new LinkedList<MapTile>();

		for (com.aceitunaproject.server.map.MapTile serverTile : from) {

			retList.add(toShared(serverTile));

		}
		return retList;
	}
}
